<?php
class Admin_LoginController extends App_Controller_Action
{
    public function indexAction()
    {
        $this->_helper->Redirector('login');
    }
    
    
    public function loginAction()
    {
        $authSession = new Zend_Session_Namespace('auth');
        if ($this->_request->isPost()) {
            $setting       = new Setting();
            $adminName      = $setting->fetchRow("name='admin_name'")->value;
            $adminPassword  = $setting->fetchRow("name='admin_password'")->value;
            $userInput = $this->_request->getPost();
            if ($userInput['username'] == $adminName  &&  $userInput['password'] == $adminPassword) {
                $authSession->isAdmin = true;
                $forward = $this->_request->getPost('forward', 'index');
                $this->_helper->Redirector(null, $forward);
            }
        } else {
            if ($authSession->isAdmin === true) {
                $this->_helper->Redirector(null, 'index');
            }
            
            $forward = $this->_request->getParam('forward');
            $this->view->forward = $forward;
        }
    }
    
    
    public function logoutAction()
    {
        $authSession = new Zend_Session_Namespace('auth');
        $authSession->unsetAll();
        $this->_helper->Redirector('login');
    }
}

